# Created by Gao Song on 2019-05-28

import os
import platform
from pathlib import Path

PID_DIR = 'd:pid'
def save_pid(app_type, app_name):
    path = Path(PID_DIR) / app_type / app_name
    path.mkdir(parents=True, exist_ok=True)
    with open(path / 'pid', mode='w') as f:
        f.write(str(os.getpid()))


def __kill_windows(app_type, app_name):
    pid = __get_pid(app_type, app_name)
    os.system(f'taskkill /F /pid {pid}')


def __kill_linux(app_type, app_name):
    pid = __get_pid(app_type, app_name)
    os.system(f'kill -9 {pid}')


def kill(app_type, app_name):
    if platform.system() == 'Windows':
        __kill_windows(app_type, app_name)
    else:
        __kill_linux(app_type, app_name)


def __get_pid(app_type, app_name):
    path = Path(PID_DIR) / app_type / app_name
    with open(path / 'pid') as f:
        pid = f.read().strip()
    return pid
